package com.ay.selfservicemachine.permission;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.ay.selfservicemachine.base.HQWAppCompatActivity;
import com.ay.selfservicemachine.databinding.DialogPermissionBinding;
import com.ay.selfservicemachine.dialog.BaseDialog;


public class PermissionDialog extends BaseDialog<DialogPermissionBinding> implements View.OnClickListener {
    HQWAppCompatActivity hqwAppCompatActivity;
    String[] PERMISSIONS;
    boolean isConstraint;
    String content;
    String content2;
    PermissionListener permissionListener;

    int frequency=0;//验证次数

    public PermissionDialog(@NonNull HQWAppCompatActivity context, String[] PERMISSIONS, boolean isConstraint,
                            String content, String content2, PermissionListener permissionListener) {
        super(context);
        this.hqwAppCompatActivity = context;
        this.PERMISSIONS = PERMISSIONS;
        this.isConstraint = isConstraint;
        this.content = content;
        this.content2 = content2;
        this.permissionListener = permissionListener;
        setCancelable(false);
        setBorderExtension();
    }


    @Override
    protected DialogPermissionBinding viewBinding() {
        return DialogPermissionBinding.inflate(getLayoutInflater());
    }


    @Override
    protected void initView() {
        super.initView();
        root.tvPermissionCancel.setOnClickListener(this);
        root.tvPermissionVerify.setOnClickListener(this);
        root.tvPermissionContent.setText(TextUtils.isEmpty(content) ? "" : content);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == root.tvPermissionCancel.getId()) {
            dismiss();
            if (permissionListener != null) {
                permissionListener.onfailure();
            }
        } else if (v.getId() == root.tvPermissionVerify.getId()) {//验证
            if (hqwAppCompatActivity!=null){
                ActivityVerify();
            }
//            if (hqwFragment!=null){
//                FragmentVerify();
//            }
        }
    }

    public void ActivityVerify() {
        hqwAppCompatActivity.HQWPermissions(PERMISSIONS, new HQWPermissionListener() {
            @Override
            public void onsucceed() {
                // 确保在主线程更新UI和关闭对话框
                root.getRoot().post(() -> {
                    if (permissionListener != null) {
                        permissionListener.onsucceed();
                        dismiss();
                    }
                });
            }

            @Override
            public void onfailure() {
                // 确保在主线程更新UI、弹出设置对话框
                root.getRoot().post(() -> {
                    if (isConstraint) {//是否进行2次请求
                        root.tvPermissionContent.setText(TextUtils.isEmpty(content2) ? "" : content2);
                        if (frequency>=1){
                           if (permissionListener != null) {
                               PermissionCommander.showPermissionSetting(getContext(), permissionListener,"");
                               dismiss();
                           }
                        }
                        frequency++;
                    } else {
                        if (permissionListener != null) {
                            permissionListener.onfailure();
                        }
                    }
                });

            }
        });

    }

//    public void FragmentVerify() {
//        hqwFragment.HQWPermissions(PERMISSIONS, new HQWPermissionListener() {
//            @Override
//            public void onsucceed() {
//                if (permissionListener != null) {
//                    permissionListener.onsucceed();
//                    dismiss();
//                }
//            }
//
//            @Override
//            public void onfailure() {
//                if (isConstraint) {//是否进行2次请求
//                    root.tvPermissionContent.setText(TextUtils.isEmpty(content2) ? "" : content2);
//                    if (frequency>=1){
//                        if (permissionListener != null) {
//                            PermissionCommander.showPermissionSetting(getContext(), permissionListener,"");
//                            dismiss();
//                        }
//                    }
//                    frequency++;
//                } else {
//                    if (permissionListener != null) {
//                        permissionListener.onfailure();
//                    }
//                }
//
//            }
//        });
//
//    }


}
